using System;
using System.Collections.Generic;
using System.Windows;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Unity;
using Silverlight.Weblog.Client.DAL.Events;
using Silverlight.Weblog.Client.DAL.ViewModel;
using Silverlight.Weblog.Client.Default.Widgets.TwitterSiderbar;
using Silverlight.Weblog.Common.IoC;
using Silverlight.Weblog.Server.DAL;

namespace Silverlight.Weblog.Client.Default.Widgets
{
    public class TwitterSidebarWidgetViewModel : ViewModelBase, ITwitterSidebarWidgetViewModel, IDisposable
    {
        [Dependency]
        public ITwitterTimeLineService TwitterTimelineService { get; set; }

        public TwitterSidebarWidgetViewModel()
        {
            IoC.Get<IEventAggregator>().GetEvent<InitiaPayloadLoadedEvent>().Subscribe(UpdateViewModel);

        }

        public void UpdateViewModel(User obj)
        {
            CurrentUser = obj;

            if (!string.IsNullOrEmpty(CurrentUser.TwitterUsername))
            {
                TwitterTimelineService.TwitterTimelineReady += new TwitterTimelineEventHandler(TwitterTimelineService_TwitterTimelineReady);
                TwitterTimelineService.GetUserTimeline(CurrentUser.TwitterUsername);
            }
        }

        void TwitterTimelineService_TwitterTimelineReady(object sender, TwitterTimelineEventHandlerArgs args)
        {
            Model = args.TwitterStatuses;
        }

        #region Implementation of IDisposable

        #region CurrentUser
        private User _currentUser;
        public User CurrentUser
        {
            get { return _currentUser; }
            set
            {
                if (_currentUser != value)
                {
                    _currentUser = value;
                    RaisePropertyChanged("CurrentUser");
                }

            }
        }
        #endregion CurrentUser

        #region Model
        private List<TwitterStatus> _model;
        public List<TwitterStatus> Model
        {
            get { return _model; }
            set
            {
                if (_model != value)
                {
                    _model = value;
                    RaisePropertyChanged("Model");
                    RaisePropertyChanged((TwitterSidebarWidgetViewModel vm) => vm.FollowMeLink);
                    RaisePropertyChanged((TwitterSidebarWidgetViewModel vm) => vm.FollowMeText);
                }

            }
        }
        #endregion Model

        public string FollowMeText
        {
            get
            {
                if (CurrentUser == null)
                    return null;

                if (string.IsNullOrEmpty(CurrentUser.TwitterUsername))
                    return "Specify twitter username";

                return CurrentUser.TwitterUsername + " Said:";
            }
        }

        #region FollowMeLink
        public Uri FollowMeLink
        {
            get
            {
                if (CurrentUser == null)
                    return null;

                return new Uri("http://twitter.com/" + CurrentUser.TwitterUsername);
            }
        }
        #endregion FollowMeLink

        public void Dispose()
        {
            IoC.Get<IEventAggregator>().GetEvent<InitiaPayloadLoadedEvent>().Unsubscribe(UpdateViewModel);
        }

        #endregion
    }
}